package com.example.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Department;
import com.example.entity.Employee;
import com.example.exception.CustomException;
import com.example.mapper.DepartmentMapper;
import com.example.service.DepartmentService;
import com.example.service.EmployeeService;
import com.example.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description TODO
 * @Author dingchunlin
 * @Date 2023/12/27 20:35
 * @Version 1.0.0
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public Department selectById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void add(Department department) {
        departmentMapper.insert(department);
    }

    @Override
    public void deleteById(Integer id) {
        departmentMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            departmentMapper.deleteById(id);
        }
    }

    @Override
    public void updateDepartment(Department department) {
        departmentMapper.updateById(department);
    }

    @Override
    public List<Department> selectAll(Department department) {
        LambdaQueryWrapper<Department> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Department::getId);
        lambdaQueryWrapper.like(ObjectUtil.isNotEmpty(department.getDepartName()),Department::getDepartName,department.getDepartName());
        List<Department> departmentList = departmentMapper.selectList(lambdaQueryWrapper);
        return departmentList;
    }

    @Override
    public PageInfo<Department> selectPage(Department department, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Department> departmentList = selectAll(department);
        departmentList.forEach( item ->{
            Integer departmentId = item.getId();
            Employee res = new Employee();
            if ("0".equals(departmentId.toString())){
                res = employeeService.selectByDepartmentIdAndRole(departmentId, RoleEnum.GENERAL_MANAGER.name());
            }else {
                res = employeeService.selectByDepartmentIdAndRole(departmentId, RoleEnum.DEPARTMENT_MANAGER.name());
            }
            if (res != null){
                item.setEmployeeName(res.getName());
            }
            else {
                item.setEmployeeName("暂无负责人");
            }

        });
        return PageInfo.of(departmentList);
    }
}
